﻿using System.Linq;
using UniMana.Utils.Controls;
using UniMana.Data;
namespace UniMana.Data
{
    public class Training_Subject_OpenInfo
    {
        #region /*Không thay đổi*/
        public bds m_bds = new bds();
        private DBMain m_data = new DBMain();
        public void Save()
        {
            m_data.m_DCSave.SubmitChanges();
        }
        public void Delete()
        {
            m_bds.DeleteCurrent();
            m_data.m_DCSave.SubmitChanges();
        }
        #endregion
        #region [Properties - Thuộc tính]
        public Training_Subject_Open Properties
        {
            get { return m_bds.Current as Training_Subject_Open; }
        }
        #endregion
        public Training_Subject_OpenInfo(int DefaultKey = 0)
        {
            var IQuery = m_data.m_DCSave.Training_Subject_Opens.Where(tb => tb.Training_SubjectOpenKey == DefaultKey);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Training_Subject_Open();
                m_bds.Add(Row);
            }
        }


        #region Chi __Hòa
        private bool m_bExist;

        public Training_Subject_OpenInfo(
            int _iTraining_SubjectKey,
            bool subject = true)
        {
            var info = this.m_data.m_DCSave.Training_Subject_Opens.Where(
                p => p.Training_SubjectKey == _iTraining_SubjectKey && p.Closed == false);
            this.m_bds.DataSource = info;
            var row = info.FirstOrDefault();
            this.m_bExist = true;
            if (row == null)
            {
                row = new Training_Subject_Open();
                this.m_bExist = false;
                row.Training_SubjectKey = _iTraining_SubjectKey;
                this.m_bds.Add(row);
                this.m_bds.MoveLast();
            }
        }

        public Training_Subject_OpenInfo(
            int _iSubjectKey,
            int _iCourseKey,
            int _iMajorKey,
            int _iLevelKey,
            int _iTrainingTypeKey)
        {
            var info = this.m_data.m_DCSave.Training_Subject_Opens.Where(
                p => p.Training_SubjectKey == _iSubjectKey &&
                    p.Catalogue_CourseKey == _iCourseKey &&
                    p.Catalogue_MajorKey == _iMajorKey &&
                    p.Catalogue_LevelKey == _iLevelKey &&
                    p.Catalogue_TrainingTypeKey == _iTrainingTypeKey);
            this.m_bds.DataSource = info;
            var row = info.FirstOrDefault();
            if (row == null)
            {
                Training_Subject_Open newSubject = new Training_Subject_Open();
                newSubject.Training_SubjectKey = _iSubjectKey;
                newSubject.Catalogue_CourseKey = _iCourseKey;
                newSubject.Catalogue_LevelKey = _iLevelKey;
                newSubject.Catalogue_TrainingTypeKey = _iTrainingTypeKey;
                newSubject.Catalogue_MajorKey = _iMajorKey;

                this.m_bds.Add(newSubject);
                this.m_bds.MoveLast();
                //row = new Training_Subject_Open();
                //this.m_bds.Add(row);
                //this.m_bds.MoveLast();
            }

        }

        public bool ExistInDataBase
        {
            get { return this.m_bExist; }
        }
        #endregion
    }
}